﻿/* *************************************************
 *  Copyright © FlexDev 2010. All rights reserved.
 *  Author: Rodrigo da Silva Brito
 *  Date: 07/16/2010
 *  Contact: mailto:rodrigosilvabrito@hotmail.com
 *  Site: http://www.flexdev.com.br
 * *************************************************
*/
using System.ServiceModel.Dispatcher;
using Flexdev.Security;

namespace Flexdev.Wcf.Extensions.Silverlight
{
    public class FlexdevMessageInspector : IClientMessageInspector
    {
        private string userName = string.Empty, password = string.Empty;

        public FlexdevMessageInspector(string userName, string password)
        {
            this.userName = CryptService.Encrypt(userName, FlexdevHeader.PasswordKey, AesCryptographyLevel.AES_256);
            this.password = CryptService.Encrypt(password, FlexdevHeader.PasswordKey, AesCryptographyLevel.AES_256);
        }

        #region IClientMessageInspector Members

        public void AfterReceiveReply(ref System.ServiceModel.Channels.Message reply, object correlationState)
        {
        }

        public object BeforeSendRequest(ref System.ServiceModel.Channels.Message request, System.ServiceModel.IClientChannel channel)
        {
            request.Headers.Add(new FlexdevHeader(userName, password));
            return null;
        }

        #endregion
    }
}
